package cn.xiu.drugstore.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.xiu.drugstore.dao.BillDao;
import cn.xiu.drugstore.pojo.Bill;
import cn.xiu.drugstore.util.BaseDao;

/**
 * 账单功能实现类
 * 
 * @author Administrator
 * 
 */
public class BillDaoImpl extends BaseDao implements BillDao {
	Bill bill = null;

	// 根据id查账单
	@Override
	public Bill queryBillByNId(int billId) throws Exception {

		StringBuffer sql = new StringBuffer();
		sql.append("select bill_id,bill_time,bill_money,");
		sql.append("bill_custid, bill_accountsid,bill_state");
		sql.append(" from bill where bill_id=?");
		Object[] params = { billId };
		ResultSet rs = super.executeQuery(sql.toString(), params);
		if (rs != null & rs.next()) {
			bill = new Bill();
			bill.setBillId(rs.getInt("bill_id"));
			bill.setBillTime(rs.getDate("bill_time"));
			bill.setBillMoney(rs.getDouble("bill_money"));
			bill.setBillCustId(rs.getInt("bill_custid"));
			bill.setAccountsId(rs.getInt("bill_accountsid"));
			bill.setState(rs.getInt("bill_state"));
		}
		return bill;
	}

	@Override
	public int addBill(Bill bill) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO bill(bill_id,bill_time,bill_money,");
		sql.append("bill_custid, bill_accountsid,bill_state)");
		sql.append(" VALUES (?,?,?,?,?,?)");
		Object[] params = { bill.getBillId(), bill.getBillTime(),
				bill.getBillMoney(), bill.getBillCustId(),
				bill.getAccountsId(), bill.getState() };
		return super.executeUpdate(sql.toString(), params);
	}

	@Override
	public int deleteBill(Bill bill) throws Exception {
		int row = 0;
		String sql = "DELETE FROM bill WHERE bill_id=?";
		Object[] params = { bill.getBillId() };
		try {
			row = this.executeUpdate(sql, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.closeResource(null);
		return row;
	}

	@Override
	public List<Bill> CheckDailyBill(String start, String over)
			throws Exception {
		StringBuffer sql = new StringBuffer();
		List<Bill> list = new ArrayList<Bill>();
		sql.append("select bill_id,bill_time,bill_money,");
		sql.append("bill_custid, bill_accountsid,bill_state");
		sql.append(" from bill  WHERE bill_time>=? AND bill_time<=?");
		Object[] params = { start, over };
		ResultSet rs = super.executeQuery(sql.toString(), params);
		while (rs != null & rs.next()) {
			bill = new Bill();
			bill.setBillId(rs.getInt("bill_id"));
			bill.setBillTime(rs.getDate("bill_time"));
			bill.setBillMoney(rs.getDouble("bill_money"));
			bill.setBillCustId(rs.getInt("bill_custid"));
			bill.setAccountsId(rs.getInt("bill_accountsid"));
			bill.setState(rs.getInt("bill_state"));
			list.add(bill);
		}
		return list;

	}
}
